package com.example.livetempogara;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Timer;

import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;

public class Controller{
	String readerAddr;
	int tagCounter;
	RilContainer output;
	ArrayList<String> listTags;
	int refresh;
	boolean pause;
	
	public Controller(){
		tagCounter=0;
		readerAddr = new String();
		this.listTags = new ArrayList<String>();
		refresh=0;
		pause=false;
	}
	
	public Controller(String readerAddr, int tagCounter){
		this.readerAddr = readerAddr;
		this.tagCounter = tagCounter;
		this.output = new RilContainer();
		this.listTags = new ArrayList<String>();
		refresh=0;
	}
	
	
	
	public class RilContainer {
		public String content;
	}
	
	private String download(String address) {
		String output;
		OutputStream out;
		InputStream in;
		output = null;
		out = null;
		in = null;
		try {
			URL url = new URL(address);
			out = new ByteArrayOutputStream();
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setConnectTimeout(2000);
			conn.setReadTimeout(2000);
			in = conn.getInputStream();
//			new BufferedInputStream(conn.getInputStream());
			
//			conn.getInputStream();
			byte buffer[] = new byte[1024];
			int numRead;
			while ((numRead = in.read(buffer)) != -1)
				out.write(buffer, 0, numRead);
			output = out.toString();
			System.out.println((new StringBuilder("Scaricato:")).append(output).toString());
		} catch (Exception exception) {
			System.out.println(exception.getStackTrace());
			// System.out.println((new
			// StringBuilder("Errore:")).append(exception.getMessage()).toString());
		} finally {
			try {
				if (in != null)
					in.close();
				if (out != null)
					out.close();
			} catch (IOException ioexception) {
			}
		}
		return output;
	}
	
	public int getData() throws IOException{
		String content = new String("");
//		content=download(readerAddr);
		content=download(readerAddr + "cgi-bin/getRil.sh?" + tagCounter);
		int readRil = 0;
		if (!content.equals("")) {
			readRil = content.split("\n").length;
			System.out.println("Letti:" + readRil + " rilevamenti");
			FileOutputStream fos = null;
//			if (content != null) {
//				fos = new FileOutputStream(Environment.getExternalStorageDirectory().getAbsolutePath()+Environment.DIRECTORY_DCIM+"/rilevamenti.tags", true);
//				fos.write(content.getBytes());
//				fos.close();
//			}
		}
		output.content = content;
		return readRil;
	}
	
	public int getLocalData() throws IOException{
		String content = new String("");
		content=download(readerAddr);
		int readRil = 0;
		if (!content.equals("")) {
			readRil = content.split("\n").length;
			System.out.println("Letti:" + readRil + " rilevamenti");
//			FileOutputStream fos = null;
//			if (content != null) {
//				fos = new FileOutputStream(Environment.getExternalStorageDirectory().getAbsolutePath()+Environment.DIRECTORY_DCIM+"/rilevamenti.tags", true);
//				fos.write(content.getBytes());
//				fos.close();
//			}
		}
		output.content = content;
		Log.v("output", content);
		return readRil;
	}
}
